********************************************************************************
* Purpose: Creates the following statistics for mothers in the year prior to births
* 1: Psychologists/psychiatrists consultation
* 2: Test for depression/mental health screening at family GP
* 3: In- and outpatient contact
********************************************************************************
do "D:\Data\workdata\708296\Project - Mother groups and mental health\Do\global.do" 

********************************************************************************
* Now generate outcomes: Monthly indicators for contact
* 1) Hospital contacts - out and inpat
********************************************************************************

// Outpatetient contact
use "$work\altpsykkontakt",clear // created in 3_cr_psyksssy
format mfr_dob %td
gen date = D_INDDTO
format date %td

drop if date == .
destring C_PATTYPE, replace 
keep if C_PATTYPE != 0
gen out = 1

bys cprm mfr_dob date: drop if _n > 1

gen month_at = floor((date - mfr_dob)/30.25) 
drop if month_at > -1
drop if month_at < -12

collapse (sum) out, by(cprm mfr_dob month_at)
replace month_at = month_at*-1
reshape wide out, i(cprm mfr_dob) j(month_at)

foreach var of varlist out* {
replace `var' = 0 if `var' == .
replace `var' = 1 if `var' > 1
rename `var' `var'_pre
}
gen out1y_pre = max(out1_pre, out2_pre, out3_pre, out4_pre, out5_pre, out6_pre, out7_pre, out8_pre, out9_pre, out10_pre, out11_pre, out12_pre) // year prior to birth indicator
compress 
save "$work\outmoms_monthly_pre", replace 


// inpatient contact
use "$work\altpsykkontakt",clear
format mfr_dob %td
gen date = D_INDDTO
format date %td

drop if date == .
destring C_PATTYPE, replace 
keep if C_PATTYPE == 0
gen inp = 1

bys cprm mfr_dob date: drop if _n > 1

gen month_at = floor((date - mfr_dob)/30.25) 
drop if month_at > -1
drop if month_at < -12

collapse (sum) inp, by(cprm mfr_dob month_at)
replace month_at = month_at*-1
reshape wide inp, i(cprm mfr_dob) j(month_at)

foreach var of varlist inp* {
replace `var' = 0 if `var' == .
replace `var' = 1 if `var' > 1
rename `var' `var'_pre
}
gen inp1y_pre = max(inp1_pre, inp2_pre, inp3_pre, inp4_pre, inp5_pre, inp6_pre, inp7_pre, inp8_pre, inp9_pre, inp10_pre, inp11_pre, inp12_pre) // year prior to birth indicator
compress 
save "$work\inmoms_monthly_pre", replace 

********************************************************************************
* 2. Psychologists/psychiatrists contacts
********************************************************************************
use "$work\psyksssymerged_cprm",clear
drop if honuge == ""
gen psyk = 1

gen int bweek = week(mfr_dob)
gen int byear = year(mfr_dob)
gen gpweek = substr(honuge, -2,2)
destring gpweek, replace
gen gpyear = substr(honuge,1,2)
destring gpyear, replace
replace gpyear = 2000 + gpyear

sort pnr mfr_dob gpweek gpyear speciale
by pnr mfr_dob gpweek gpyear speciale: drop if _n > 1

duplicates drop pnr mfr_dob gpweek gpyear speciale, force

collapse (sum) psyk, by(pnr mfr_dob gpyear gpweek)

gen gp_date = mdy(1,1,gpyear) + (gpweek-1)*7
format gp_date %td
gen month_at = floor((gp_date - mfr_dob)/30.25)
drop if month_at > -1
drop if month_at < -12
collapse (sum) psyk, by(pnr mfr_dob month_at)
replace month_at = month_at*-1
reshape wide psyk, i(pnr mfr_dob) j(month_at)

foreach var of varlist psyk*{
replace `var' = 0 if `var' == .
replace `var' = 1 if `var' > 1
rename `var' gpmw_`var'_pre
}
gen gpmw_psyk1y_pre = max(gpmw_psyk1_pre, gpmw_psyk2_pre, gpmw_psyk3_pre, gpmw_psyk4_pre, gpmw_psyk5_pre, gpmw_psyk6_pre, gpmw_psyk7_pre, gpmw_psyk8_pre, gpmw_psyk9_pre, gpmw_psyk10_pre, gpmw_psyk11_pre, gpmw_psyk12_pre)
rename pnr cprm
compress 
save "$work\psyksssy_monthly_pre",replace

********************************************************************************
* 3. GP mental health related contact
********************************************************************************
use "$work\conssssymerged_cprm",clear
drop if honuge == ""
gen cons = 1

gen int bweek = week(mfr_dob)
gen int byear = year(mfr_dob)
gen gpweek = substr(honuge, -2,2)
destring gpweek, replace
gen gpyear = substr(honuge,1,2)
destring gpyear, replace
replace gpyear = 2000 + gpyear

sort pnr mfr_dob gpweek gpyear speciale
by pnr mfr_dob gpweek gpyear speciale: drop if _n > 1

duplicates drop pnr mfr_dob gpweek gpyear speciale, force

collapse (sum) cons, by(pnr mfr_dob gpyear gpweek)

gen gp_date = mdy(1,1,gpyear) + (gpweek-1)*7
format gp_date %td
gen month_at = floor((gp_date - mfr_dob)/30.25) 
drop if month_at > -1
drop if month_at < -12

collapse (sum) cons, by(pnr mfr_dob month_at)
replace month_at = month_at*-1
reshape wide cons, i(pnr mfr_dob) j(month_at)

foreach var of varlist cons* {
replace `var' = 0 if `var' == .
replace `var' = 1 if `var' > 1
rename `var' gpmw_`var'_pre
}
gen gpmw_cons1y_pre = max(gpmw_cons1_pre, gpmw_cons2_pre, gpmw_cons3_pre, gpmw_cons4_pre, gpmw_cons5_pre, gpmw_cons6_pre, gpmw_cons7_pre, gpmw_cons8_pre, gpmw_cons9_pre, gpmw_cons10_pre, gpmw_cons11_pre, gpmw_cons12_pre)
rename pnr cprm
compress 
save "$work\conssssy_monthly_pre",replace
